import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { Toast } from "vant";

const routes: Array<RouteRecordRaw> = [
 {
   path: '/',
   redirect: '/home'
 },
 {
   path: '/home',
   name: 'Home',
   component: () => import('../views/Home/home.vue'),
   meta: {
    meta: true
   }
 },
 {
   path: '/topic',
   name: 'Topic',
   component: () => import('../views/Topic/Topic.vue'),
   meta: {
    meta: true
   }
 },
 {
  path: '/category',
  name: 'Category',
  component: () => import('../views/Category/Category.vue'),
  meta: {
    meta: true
   }
},
{
  path: '/cart',
  name: 'Cart',
  component: () => import('../views/Cart/Cart.vue'),
  meta: {
    meta: true
   }
},
{
  path: '/user',
  name: 'User',
  component: () => import('../views/User/User.vue'),
  meta: {
    meta: true
   }
},
{
  path: '/home/search',
  name: 'Search',
  component: () => import('../views/Search/Search.vue'),
  meta: {
    meta: false
   }
},
{
  path: '/channel',
  name: 'Channel',
  component: () => import('../views/Channel.vue')
},
{
  path: '/brand',
  name: 'Brand',
  component: () => import('../views/brand.vue')
},
{
  path: '/detail',
  name: 'Detail',
  component: () => import('../views/Detail/detail.vue')
}

]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from) => {
  console.log(to);
  const token = localStorage.getItem('token')
  if (!token) {
    if (to.path === '/cart') {
      Toast.loading({
        message: '请先登录',
        forbidClick: true,
      });
      router.push('/user')
    }
  }
})

export default router
